package com.lianniu.web.tea.user.register.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.web.base.BaseResult;
import com.lianniu.web.baseService.user.model.FrontUserVo;
import com.lianniu.web.baseService.user.service.IFrontUserService;
import com.lianniu.web.tea.no.service.ITeaNoService;
import com.lianniu.web.tea.user.addr.model.TeaUserAddr;
import com.lianniu.web.tea.user.addr.service.ITeaUserAddrService;
import com.lianniu.web.tea.user.register.service.ITeaUserRegisterService;
import com.lianniu.web.util.MD5Util;
import com.lianniu.web.util.TeaContant;

@Controller
@RequestMapping("/tea/rest/user")
public class TeaRestUserRegisterController {

	private final static Logger logger = Logger.getLogger(TeaRestUserRegisterController.class);

	@Autowired
	private IFrontUserService frontUserService;
	
	@Autowired
	private ITeaUserAddrService teaUserAddrService;
	
	@Autowired
	private ITeaNoService teaNoService;
	
	@Autowired
	private ITeaUserRegisterService teaUserRegisterService;

	@RequestMapping(value = { "/register" }, method = RequestMethod.POST)
	@Transactional
	public @ResponseBody BaseResult buyerAdd(HttpServletRequest request, FrontUserVo frontUserVo) {
		logger.info("TeaRestUserRegisterController register begin.");
		logger.info("TeaRestUserRegisterController register:" + JSON.toJSONString(frontUserVo));
		try {
			logger.info("buyerAdd begin.");
			/*String buyerName = request.getParameter("buyerName");
			String compName = request.getParameter("compName");
			String address = request.getParameter("address");*/
			String mobile = request.getParameter("phone");
			String password = request.getParameter("passWord");
			String nickName = request.getParameter("nickName");
			String linkman = request.getParameter("linkMan");
			
			String userArea = request.getParameter("selectAddr");
			String userAddr = request.getParameter("detailAddr");
			
			if (password != null) {
				String loginPassWord = MD5Util.getMD5String(password);
				frontUserVo.setLoginPassWord(loginPassWord);
			}
			frontUserVo.setMobile(mobile);
			frontUserVo.setNickName(nickName);
			frontUserVo.setRealName(linkman);
			frontUserVo.setDelFlag("1");
			frontUserVo.setUserName(mobile);
			frontUserVo.setCategoryId("1");
			//int userId = teaNoService.getTeaNo(TeaContant.NO_USER_ID);
			//frontUserVo.setUsersId(Long.valueOf(userId));
			
			BaseResult baseResult = frontUserService.insertFrontUser(frontUserVo);
			
			TeaUserAddr teaUserAddr = new TeaUserAddr();
			teaUserAddr.setIsDefault(TeaContant.DEFAULT);
			teaUserAddr.setUserName(linkman);
			teaUserAddr.setUserPhone(mobile);
			teaUserAddr.setUserArea(userArea);
			teaUserAddr.setUserAddr(userAddr);
			teaUserAddr.setUserId(Long.toString(frontUserVo.getUsersId()));
			teaUserAddrService.insert(teaUserAddr);
			/*BuyerinfoVo buyer = new BuyerinfoVo();
			buyer.setUserId(frontUserVo.getUsersId());
			buyer.setProvince(frontUserVo.getLiveProvince());
			buyer.setCity(frontUserVo.getLiveCity());
			buyer.setCounty(frontUserVo.getLiveCounty());
			buyer.setBuyerName(buyerName);
			buyer.setCompName(compName);
			buyer.setBuyerTel(mobile);
			buyer.setAddress(address);
			buyer.setDelFlag("1");
			buyer.setBuyerStatus("1");*/
			// buyerService.insertBuyerinfoVo(buyer);
			//logger.info("buyerAdd baseResult:" + JSON.toJSONString(baseResult));
			//logger.info("buyerAdd end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("buyerAdd error:" + e.getMessage());
			logger.info("TeaRestUserRegisterController register end.");
			return BaseResult.exception(e.getMessage());
		}
	}

	@RequestMapping(value = "/registerWeixin" ,method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody BaseResult registerWeixin(HttpServletRequest request) {
		try {
			String openid = request.getParameter("openid");
			String username = request.getParameter("username");
			String picpath = request.getParameter("picpath");
			BaseResult baseResult =teaUserRegisterService.registerWeixin(openid,username,picpath);
			return baseResult;
		} catch (Exception e) {
			logger.error("buyerAdd error:" + e.getMessage());
			logger.info("TeaRestUserRegisterController register end.");
			return BaseResult.exception(e.getMessage());
		}
	}
}
